using System;
using System.Text;

namespace nmerge
{
    class ArgumentParser
    {
        private ArgumentParser()
        {           
        }

        public static ArgumentParser Create(string[] args)
        {
            var parser = new ArgumentParser();
            parser.Parse(args);
            return parser;
        }

        private void Parse(string[] args)
        {
            try
            {
                if(args.Length != 2 && args.Length != 3)
                    throw new ArgumentException("Only 2 or 3 parameters are expected.");

                var inputFiles = args[0];
                InputFilePathList = inputFiles.Split(';');

                var outputFile = args[1];
                OutputFilePath = outputFile;

                if (args.Length == 3)
                {
                    OutputFileEncoding = Encoding.GetEncoding(args[2]);
                }
                else
                {
                    OutputFileEncoding = Encoding.Unicode;
                }

                Invalid = false;
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to parse parameter: {0}", e.Message);
                Invalid = true;
            }
        }

        public bool Invalid { get; private set; }

        public string OutputFilePath { get; private set; }
        public string[] InputFilePathList { get; private set; }
        public Encoding OutputFileEncoding { get; private set; }
    }
}